﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Silvermoon.Controls
{
    public enum ItemChangedEventType
    {
        ItemAdded,
        ItemRemoved,
        Reset
    }

    public class ItemChangedEventArgs : EventArgs
    {
        public ItemChangedEventType Type { get; private set; }
        public int Index { get; private set; }

        public ItemChangedEventArgs(ItemChangedEventType type, int index)
            : base()
        {
            this.Type = type;
            this.Index = index;
        }
    }

    public class ItemCollection : Collection<Item>
    {
        protected override void RemoveItem(int index)
        {
            Item item = this[index];
            base.RemoveItem(index);
            item.Dispose();
            OnChanged(ItemChangedEventType.ItemRemoved, index);

        }

        protected override void InsertItem(int index, Item item)
        {
            base.InsertItem(index, item);
            OnChanged(ItemChangedEventType.ItemAdded, index);
        }

        protected override void ClearItems()
        {
            foreach (Item item in this) item.Dispose();
            base.ClearItems();
            OnChanged(ItemChangedEventType.Reset, 0);
        }

        protected virtual void OnChanged(ItemChangedEventType type, int index)
        {
            if (ListChanged != null) ListChanged(this, new ItemChangedEventArgs(type, index));
        }

        public event EventHandler<ItemChangedEventArgs> ListChanged;

    }
}
